//
//  GQHFinalExpenseController.m
//  Xanthium
//
//  Created by GuanQinghao on 14/05/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHFinalExpenseController.h"
#import "GQHFinalExpenseView.h"
#import "GQHContractPaymentModel.h"
#import "GQHHeader.h"

@interface GQHFinalExpenseController () <GQHFinalExpenseViewDelegate> {
    
#pragma mark Data
    NSString *finalGasID;
    NSString *finalETCID;
    NSString *finalCashID;
#pragma mark View
    /// 自定义根视图
    GQHFinalExpenseView *rootView;
}

@end


@implementation GQHFinalExpenseController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHFinalExpenseView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    NSArray *prepaymentArray = _qh_orderModel.qh_contractPrepayment;
    
    for (GQHContractPaymentModel*paymentModel in prepaymentArray) {
        
        if ([paymentModel.qh_paymentType isEqualToString:@"01"]) {
            
            [rootView.qh_contentArray qh_safetySetObject:paymentModel.qh_paymentAmount atIndex:0];
        }
        if ([paymentModel.qh_paymentType isEqualToString:@"02"]) {
            
            [rootView.qh_contentArray qh_safetySetObject:paymentModel.qh_paymentAmount atIndex:1];
        }
        if ([paymentModel.qh_paymentType isEqualToString:@"03"]) {
            
            [rootView.qh_contentArray qh_safetySetObject:paymentModel.qh_paymentAmount atIndex:2];
        }
        [rootView.qh_contentArray qh_safetySetObject:_qh_orderModel.qh_contractPrepaymentSum atIndex:3];
    }
    
    NSArray *finalpaymentArray = _qh_orderModel.qh_contractFinalPayment;
    
    for (GQHContractPaymentModel*paymentModel in finalpaymentArray) {
        
        if ([paymentModel.qh_paymentType isEqualToString:@"01"]) {
            
            [rootView.qh_contentArray qh_safetySetObject:paymentModel.qh_paymentAmount atIndex:4];
            finalGasID = paymentModel.qh_paymentID;
        }
        if ([paymentModel.qh_paymentType isEqualToString:@"02"]) {
            
            [rootView.qh_contentArray qh_safetySetObject:paymentModel.qh_paymentAmount atIndex:5];
            finalETCID = paymentModel.qh_paymentID;
        }
        if ([paymentModel.qh_paymentType isEqualToString:@"03"]) {
            
            [rootView.qh_contentArray qh_safetySetObject:paymentModel.qh_paymentAmount atIndex:6];
            finalCashID = paymentModel.qh_paymentID;
        }
        [rootView.qh_contentArray qh_safetySetObject:_qh_orderModel.qh_contractFinalPaymentSum atIndex:7];
    }
    /*
    NSArray *otherpaymentArray = _qh_orderModel.qh_contractOtherExpense;
    
    for (GQHContractPaymentModel*paymentModel in otherpaymentArray) {
        
        if ([paymentModel.qh_paymentType isEqualToString:@"01"]) {
            
            [rootView.qh_contentArray qh_safetySetObject:paymentModel.qh_paymentAmount atIndex:8];
            otherGasID = paymentModel.qh_paymentID;
        }
        if ([paymentModel.qh_paymentType isEqualToString:@"02"]) {
            
            [rootView.qh_contentArray qh_safetySetObject:paymentModel.qh_paymentAmount atIndex:9];
            otherETCID = paymentModel.qh_paymentID;
        }
        if ([paymentModel.qh_paymentType isEqualToString:@"03"]) {
            
            [rootView.qh_contentArray qh_safetySetObject:paymentModel.qh_paymentAmount atIndex:10];
            otherCashID = paymentModel.qh_paymentID;
        }
        [rootView.qh_contentArray qh_safetySetObject:_qh_orderModel.qh_contractPrepaymentSum atIndex:11];
    }
     */
    
    float prepaymentSum = [_qh_orderModel.qh_contractPrepaymentSum floatValue];
    float finalpaymentSum = [_qh_orderModel.qh_contractFinalPaymentSum floatValue];
    float totalpayment = prepaymentSum + finalpaymentSum;
    [rootView.qh_contentArray qh_safetySetObject:[NSString stringWithFormat:@"%.2f",totalpayment] atIndex:13];
    
    NSArray *imageArray = [_qh_orderModel.qh_attachment componentsSeparatedByString:@","];
    rootView.qh_imageArray = [NSMutableArray arrayWithArray:imageArray];
    [rootView.qh_tableView reloadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"尾款信息";
    [self.qh_rightButton setTitle:@"确认" forState:UIControlStateNormal];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case 2: {
            
            switch (indexPath.row) {
                    
                case 0: {
                    
                    /// 油卡
                    [self qh_alertWithTitle:@"请输入油卡金额" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"油卡金额";
                        textField.keyboardType = UIKeyboardTypeDecimalPad;
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        
                        /// 尾款费用
                        float finalGas = [inputTextField.text floatValue];
                        float finalETC = [rootView.qh_contentArray[5] floatValue];
                        float finalCash = [rootView.qh_contentArray[6] floatValue];
                        float otherGas = [rootView.qh_contentArray[8] floatValue];
                        float otherETC = [rootView.qh_contentArray[9] floatValue];
                        float otherCash = [rootView.qh_contentArray[10] floatValue];
                        
                        /// 预付款、尾款、其他费用
                        float preTotal = [_qh_orderModel.qh_contractPrepaymentSum floatValue];
                        float finalTotal = finalGas + finalETC +finalCash;
                        float otherTotal = otherGas + otherETC + otherCash;
                        
                        /// 总计
                        float total = preTotal + finalTotal + otherTotal;
                        
                        /// 尾款款和总计更新
                        NSString *finalTotalString = [NSString stringWithFormat:@"%.2f",finalTotal];
                        [rootView.qh_contentArray qh_safetySetObject:finalTotalString atIndex:7];
                        NSString *totalString = [NSString stringWithFormat:@"%.2f",total];
                        [rootView.qh_contentArray qh_safetySetObject:totalString atIndex:13];
                        
                        [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:4];
                        [rootView.qh_tableView reloadData];
                    } completion:nil];
                }
                    break;
                case 1: {
                    
                    /// ETC
                    [self qh_alertWithTitle:@"请输入ETC金额" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"ETC金额";
                        textField.keyboardType = UIKeyboardTypeDecimalPad;
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        
                        /// 尾款费用
                        float finalGas = [rootView.qh_contentArray[4] floatValue];
                        float finalETC = [inputTextField.text floatValue];
                        float finalCash = [rootView.qh_contentArray[6] floatValue];
                        float otherGas = [rootView.qh_contentArray[8] floatValue];
                        float otherETC = [rootView.qh_contentArray[9] floatValue];
                        float otherCash = [rootView.qh_contentArray[10] floatValue];
                        
                        /// 预付款、尾款、其他费用
                        float preTotal = [_qh_orderModel.qh_contractPrepaymentSum floatValue];
                        float finalTotal = finalGas + finalETC +finalCash;
                        float otherTotal = otherGas + otherETC + otherCash;
                        
                        /// 总计
                        float total = preTotal + finalTotal + otherTotal;
                        
                        /// 尾款款和总计更新
                        NSString *finalTotalString = [NSString stringWithFormat:@"%.2f",finalTotal];
                        [rootView.qh_contentArray qh_safetySetObject:finalTotalString atIndex:7];
                        NSString *totalString = [NSString stringWithFormat:@"%.2f",total];
                        [rootView.qh_contentArray qh_safetySetObject:totalString atIndex:13];
                        
                        [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:5];
                        [rootView.qh_tableView reloadData];
                    } completion:nil];
                }
                    break;
                case 2: {
                    
                    /// 现金
                    [self qh_alertWithTitle:@"请输入现金金额" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"现金金额";
                        textField.keyboardType = UIKeyboardTypeDecimalPad;
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        
                        /// 尾款费用
                        float finalGas = [rootView.qh_contentArray[4] floatValue];
                        float finalETC = [rootView.qh_contentArray[5] floatValue];
                        float finalCash = [inputTextField.text floatValue];
                        float otherGas = [rootView.qh_contentArray[8] floatValue];
                        float otherETC = [rootView.qh_contentArray[9] floatValue];
                        float otherCash = [rootView.qh_contentArray[10] floatValue];
                        
                        /// 预付款、尾款、其他费用
                        float preTotal = [_qh_orderModel.qh_contractPrepaymentSum floatValue];
                        float finalTotal = finalGas + finalETC +finalCash;
                        float otherTotal = otherGas + otherETC + otherCash;
                        
                        /// 总计
                        float total = preTotal + finalTotal + otherTotal;
                        
                        /// 尾款款和总计更新
                        NSString *finalTotalString = [NSString stringWithFormat:@"%.2f",finalTotal];
                        [rootView.qh_contentArray qh_safetySetObject:finalTotalString atIndex:7];
                        NSString *totalString = [NSString stringWithFormat:@"%.2f",total];
                        [rootView.qh_contentArray qh_safetySetObject:totalString atIndex:13];
                        
                        [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:6];
                        [rootView.qh_tableView reloadData];
                    } completion:nil];
                }
                    break;
                default:
                    break;
            }
        }
            break;
        case 3: {
            
            switch (indexPath.row) {
                    
                case 0: {
                    
                    /// 油卡
                    [self qh_alertWithTitle:@"请输入油卡金额" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"油卡金额";
                        textField.keyboardType = UIKeyboardTypeDecimalPad;
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        
                        /// 其他费用
                        float finalGas = [rootView.qh_contentArray[4] floatValue];
                        float finalETC = [rootView.qh_contentArray[5] floatValue];
                        float finalCash = [rootView.qh_contentArray[6] floatValue];
                        float otherGas = [inputTextField.text floatValue];
                        float otherETC = [rootView.qh_contentArray[9] floatValue];
                        float otherCash = [rootView.qh_contentArray[10] floatValue];
                        
                        /// 预付款、尾款、其他费用
                        float preTotal = [_qh_orderModel.qh_contractPrepaymentSum floatValue];
                        float finalTotal = finalGas + finalETC +finalCash;
                        float otherTotal = otherGas + otherETC + otherCash;
                        
                        /// 总计
                        float total = preTotal + finalTotal + otherTotal;
                        
                        /// 其他费用和总计更新
                        NSString *otherTotalString = [NSString stringWithFormat:@"%.2f",otherTotal];
                        [rootView.qh_contentArray qh_safetySetObject:otherTotalString atIndex:11];
                        NSString *totalString = [NSString stringWithFormat:@"%.2f",total];
                        [rootView.qh_contentArray qh_safetySetObject:totalString atIndex:13];
                        
                        [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:8];
                        [rootView.qh_tableView reloadData];
                    } completion:nil];
                }
                    break;
                case 1: {
                    
                    /// ETC
                    [self qh_alertWithTitle:@"请输入ETC金额" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"ETC金额";
                        textField.keyboardType = UIKeyboardTypeDecimalPad;
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        
                        /// 其他费用
                        float finalGas = [rootView.qh_contentArray[4] floatValue];
                        float finalETC = [rootView.qh_contentArray[5] floatValue];
                        float finalCash = [rootView.qh_contentArray[6] floatValue];
                        float otherGas = [rootView.qh_contentArray[8] floatValue];
                        float otherETC = [inputTextField.text floatValue];
                        float otherCash = [rootView.qh_contentArray[10] floatValue];
                        
                        /// 预付款、尾款、其他费用
                        float preTotal = [_qh_orderModel.qh_contractPrepaymentSum floatValue];
                        float finalTotal = finalGas + finalETC +finalCash;
                        float otherTotal = otherGas + otherETC + otherCash;
                        
                        /// 总计
                        float total = preTotal + finalTotal + otherTotal;
                        
                        /// 其他费用和总计更新
                        NSString *otherTotalString = [NSString stringWithFormat:@"%.2f",otherTotal];
                        [rootView.qh_contentArray qh_safetySetObject:otherTotalString atIndex:11];
                        NSString *totalString = [NSString stringWithFormat:@"%.2f",total];
                        [rootView.qh_contentArray qh_safetySetObject:totalString atIndex:13];
                        
                        [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:9];
                        [rootView.qh_tableView reloadData];
                    } completion:nil];
                }
                    break;
                case 2: {
                    
                    /// 现金
                    [self qh_alertWithTitle:@"请输入现金金额" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"现金金额";
                        textField.keyboardType = UIKeyboardTypeDecimalPad;
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        
                        /// 其他费用
                        float finalGas = [rootView.qh_contentArray[4] floatValue];
                        float finalETC = [rootView.qh_contentArray[5] floatValue];
                        float finalCash = [rootView.qh_contentArray[6] floatValue];
                        float otherGas = [rootView.qh_contentArray[8] floatValue];
                        float otherETC = [rootView.qh_contentArray[9] floatValue];
                        float otherCash = [inputTextField.text floatValue];
                        
                        /// 预付款、尾款、其他费用
                        float preTotal = [_qh_orderModel.qh_contractPrepaymentSum floatValue];
                        float finalTotal = finalGas + finalETC +finalCash;
                        float otherTotal = otherGas + otherETC + otherCash;
                        
                        /// 总计
                        float total = preTotal + finalTotal + otherTotal;
                        
                        /// 其他费用和总计更新
                        NSString *otherTotalString = [NSString stringWithFormat:@"%.2f",otherTotal];
                        [rootView.qh_contentArray qh_safetySetObject:otherTotalString atIndex:11];
                        NSString *totalString = [NSString stringWithFormat:@"%.2f",total];
                        [rootView.qh_contentArray qh_safetySetObject:totalString atIndex:13];
                        
                        [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:10];
                        [rootView.qh_tableView reloadData];
                    } completion:nil];
                }
                    break;
                case 4: {
                    
                    /// 备注
                    [self qh_alertWithTitle:@"请输入备注" message:nil inputHandler:^(UITextField * _Nullable textField) {
                        
                        textField.placeholder = @"请输入备注";
                        
                    } actionHandler:^(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController) {
                        
                        UITextField *inputTextField = alertController.textFields.firstObject;
                        
                        [rootView.qh_contentArray qh_safetySetObject:inputTextField.text atIndex:12];
                        [rootView.qh_tableView reloadData];
                    } completion:nil];
                }
                    break;
                default:
                    break;
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark --TargetMethod
- (void)qh_clickedRightButton:(UIButton *)sender
{
    for (int i = 0; i < rootView.qh_contentArray.count; i ++)
    {
        NSString * str = rootView.qh_contentArray[i];
        if ([str isEqualToString:@""])
        {
            [rootView.qh_contentArray replaceObjectAtIndex:i withObject:@"0"];
        }
    }
    
    if ([rootView.qh_contentArray[13] floatValue] == 0)
    {
        [self showToastWithText:@"运费总金额不能为0"];
        return;
    }
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:USER_GET_ID forKey:@"ownerId"];
    [parameters setObject:_qh_orderModel.qh_orderNumber forKey:@"waybillAwb"];
    [parameters setObject:rootView.qh_contentArray[12] forKey:@"remark"];
    
    NSMutableArray *finalpaymentArray = [NSMutableArray array];
    NSMutableDictionary *finalGasDictionary = [NSMutableDictionary dictionary];
    [finalGasDictionary setObject:@"01" forKey:@"paymentType"];
    [finalGasDictionary setObject:rootView.qh_contentArray[4] forKey:@"amount"];
    [finalGasDictionary setObject:@"01" forKey:@"paymentTerms"];
    [finalGasDictionary setObject:@"" forKey:@"reserved1"];
    [finalGasDictionary setObject:finalGasID forKey:@"id"];
    [finalpaymentArray addObject:finalGasDictionary];
    
    NSMutableDictionary *finalETCDictionary = [NSMutableDictionary dictionary];
    [finalETCDictionary setObject:@"02" forKey:@"paymentType"];
    [finalETCDictionary setObject:rootView.qh_contentArray[5] forKey:@"amount"];
    [finalETCDictionary setObject:@"01" forKey:@"paymentTerms"];
    [finalETCDictionary setObject:@"" forKey:@"reserved1"];
    [finalETCDictionary setObject:finalETCID forKey:@"id"];
    [finalpaymentArray addObject:finalETCDictionary];
    
    NSMutableDictionary *preCashDictionary = [NSMutableDictionary dictionary];
    [preCashDictionary setObject:@"03" forKey:@"paymentType"];
    [preCashDictionary setObject:rootView.qh_contentArray[6] forKey:@"amount"];
    [preCashDictionary setObject:@"01" forKey:@"paymentTerms"];
    [preCashDictionary setObject:@"" forKey:@"reserved1"];
    [preCashDictionary setObject:finalCashID forKey:@"id"];
    [finalpaymentArray addObject:preCashDictionary];
    
    NSData *finalJsonData = [NSJSONSerialization dataWithJSONObject:finalpaymentArray options:NSJSONWritingPrettyPrinted error:nil];
    NSString *finalJsonString = [[NSString alloc] initWithData:finalJsonData encoding:NSUTF8StringEncoding];
    [parameters setObject:finalJsonString forKey:@"jsonString1"];
    
    
    NSMutableArray *otherpaymentArray = [NSMutableArray array];
    NSMutableDictionary *otherGasDictionary = [NSMutableDictionary dictionary];
    [otherGasDictionary setObject:@"01" forKey:@"paymentType"];
    [otherGasDictionary setObject:rootView.qh_contentArray[8] forKey:@"amount"];
    [otherGasDictionary setObject:@"01" forKey:@"paymentTerms"];
    [otherGasDictionary setObject:@"" forKey:@"reserved1"];
    [otherGasDictionary setObject:@"" forKey:@"id"];
    [otherpaymentArray addObject:otherGasDictionary];
    
    NSMutableDictionary *otherETCDictionary = [NSMutableDictionary dictionary];
    [otherETCDictionary setObject:@"02" forKey:@"paymentType"];
    [otherETCDictionary setObject:rootView.qh_contentArray[9] forKey:@"amount"];
    [otherETCDictionary setObject:@"01" forKey:@"paymentTerms"];
    [otherETCDictionary setObject:@"" forKey:@"reserved1"];
    [otherETCDictionary setObject:@"" forKey:@"id"];
    [otherpaymentArray addObject:otherETCDictionary];
    
    NSMutableDictionary *otherCashDictionary = [NSMutableDictionary dictionary];
    [otherCashDictionary setObject:@"03" forKey:@"paymentType"];
    [otherCashDictionary setObject:rootView.qh_contentArray[10] forKey:@"amount"];
    [otherCashDictionary setObject:@"01" forKey:@"paymentTerms"];
    [otherCashDictionary setObject:@"" forKey:@"reserved1"];
    [otherCashDictionary setObject:@"" forKey:@"id"];
    [otherpaymentArray addObject:otherCashDictionary];
    
    NSData *otherJsonData = [NSJSONSerialization dataWithJSONObject:otherpaymentArray options:NSJSONWritingPrettyPrinted error:nil];
    NSString *otherJsonString = [[NSString alloc] initWithData:otherJsonData encoding:NSUTF8StringEncoding];
    [parameters setObject:otherJsonString forKey:@"jsonString2"];
    
    
    NSString *APIURLString = [[NSString qh_APIURLString] stringByAppendingPathComponent:@"/buWaybill/1/noBilling"];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [GQHNetHelper qh_requestGET:APIURLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        DLog(@"%@---%@---%@",code,message,data);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (0 == [code integerValue]) {
            
            [self showToastWithText:message];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [self.navigationController popViewControllerAnimated:YES];
            });
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}

#pragma mark --PrivateMethod

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
